#!/usr/bin/wish -f
# fsvue source for the Tk toolkit
# Basic font for button, menus and labels
# Product of NVI,Inc./GSFC (2000)
# to be incorporated into the VLBI Field System.
# Window manager configurations

# Use a small font for help all the time.

  set ICONDIR /usr2/fs/fsvue/images
  set FSDIR /usr2/fs
  set BINDIR /usr2/fs/bin
  set LIBDIR /usr2/fs/fsvue/lib
  set LIBDEF /usr2/fs/fsvue/def_lib
  set LOGDIR /usr2/log

  set vieX [winfo screenwidth .]
  set vieY [winfo screenheight .]  

  global tkVersion
  wm positionfrom . user
  wm sizefrom . ""
  wm maxsize . 1152 900
  wm iconposition . 1200 500
  wm iconbitmap . @$ICONDIR/vlb.icon
  wm iconname . {fsvue}
#position window full screen from top left corner.
#  wm geometry . $vieX\x$vieY+0+0
#position window from top left corner.
  wm geometry . +0+0
  wm title . {FS VLBI User Environment}
  set w .
  $w config -cursor {top_left_arrow black white}

  global inpipe stopscroll


# Decide on font size.
  if {$argv=="D"} {
#      exec $LIBDEF/fsvue_err 1 &
      set bfont variable
      set fontsize $bfont
  } elseif {$argv=="F"} {
      set bfont [exec $LIBDEF/vue_sysfonts {}]
      set fontsize $bfont
      set ibfont [open $LIBDEF/my_font w+]
      puts $ibfont $bfont
      catch {close $ibfont}
  } else { 
      set ibfont [open "|/bin/sh -c \"cat $LIBDEF/my_font; :\""]
      set bfont [gets $ibfont]
      set newfile_filename [lindex $argv 1]
      set fontsize $bfont
      catch {close $ibfont}
  }

  # Use an associative array to store keys and commands
  set cmd(Field_System) "xterm -geom 60x10+0+0 -sb -sl 1000 -e $BINDIR/fs"
  set cmd(FS_Console) "xterm -geom 60x5+0+650 -e $BINDIR/oprin"
  set cmd(Send_mail) "$LIBDEF/vue_fsmail $fontsize"
  set cmd(Netscape_mail) "netscape -mail"
  set cmd(FS_Ready_Form) "$FSDIR/msg/msg"
  set cmd(lognm) "xterm -bg black -fg white -sb -sl 1000 -e $BINDIR/lognm"
  set cmd(pfmed) "xterm -bg black -fg white -sb -sl 1000 -e $BINDIR/pfmed"
  set cmd(setcl) "xterm -bg black -fg white -sb -sl 1000 -e $BINDIR/setcl"
  set cmd(logpl) "$BINDIR/logpl"      
  set cmd(pdplt) "xterm -bg black -fg white -sb -sl 1000 -e $BINDIR/pdplt"
  set cmd(xterm) "xterm -bg black -fg white -cr yellow -geometry +10+120 -fn 10x20"
  
  # File storage
  set xfsfile "~/.xfscmds"
  set global_startmode 0            ;# 0=Interactive mode; 1=Command-line mode
#set current_filename [lindex $argv 0]
  set current_filename ""           ;# The selected log file
  set current_DefaultDir /usr2/log  ;# Default directory for FS log files
  set stopscroll 0

# Set the log file to station.log to start out with.
  set newfile_filename "station.log" 

# Results
  set val01 [exec vueinfo time]
  set val02 $newfile_filename
  set val03 [exec vueinfo rack]
  set val04 [exec vueinfo drive1]
  set val05 [exec vueinfo drive2]
  set val06 [exec vueinfo site]
  set val07 [exec vueinfo lskd]
  set val08 [exec vueinfo ifs]

# Top frame is a menubar
# there is more magic that you can do with this
# build widget .m
frame .m \
	-bg {navy} \
	-borderwidth {2} \
	-relief {groove} 

# ==========================================================================
#
#           COOK UP THE BUTTONS AND PACK THEM SECTION
#
# ==========================================================================
# two buttons in the menu giving dropdown menus
menubutton .m.file -text "File" -relief {raise}  -menu .m.file.menu -background navy -fg white

menubutton .m.dsp -text "Displays" -relief {raise}  -menu .m.dsp.menu -background navy -fg white

menubutton .m.cmd -text "Execute" -relief {raise}  -menu .m.cmd.menu -background navy -fg white

button .m.fshelp -text "Field System HELP" -relief {raise}  -background navy -fg white -command {exec $LIBDEF/vue_fshelp $fontsize &}

button .m.fs -text "Examine LOGS" -relief {raise}  -background navy -fg white -command {exec $LIBDEF/vue_fslog $fontsize $LOGDIR/$newfile_filename &}

button .m.fso -text "Other Files" -relief {raise}  -background navy -fg white -command {exec $LIBDEF/vue_fsfiles $fontsize &}

button .m.q -text "Exit" -relief {raise}  -background red -fg white -command execAndLogClose

menubutton .m.help -bitmap @$ICONDIR/info.icon -menu .m.help.menu -relief {raise}  -menu .m.help.menu -background navy -fg white

entry .m.ent -relief {sunken} -bg linen -fg black

pack .m.help .m.file .m.dsp .m.cmd .m.ent .m.fs .m.fso -side left -fill both -expand 1
# This is pack incase we have a difficult file.
#pack .m.help .m.file .m.dsp .m.cmd .m.ent .m.logm .m.fs -side left -fill both -expand 1

# Main command entry area is under that
pack .m.fshelp .m.q  -side left -fill both -expand 1
pack .m -side top -fill x -expand 0

# Put the focus into the box
bind .m.ent <Return> {Execute}
focus .m.ent

# ==========================================================================
#
#                         DISPLAY SECTION
#
# ==========================================================================
proc displayWin { } {
    global val01 val02 val03 val04 val05 val06 val07 val08
    global txt01 txt02 txt03 txt04 txt05 txt06 txt07 txt08
    global bfont

    frame .m1 \
	    -bg {blue} \
	    -borderwidth {5} \
	    -relief {groove} 

    label .m1.txt02  -background blue -fg white -text "SCHEDULE"
    label .m1.txt03  -background blue -fg white -text "RACK"
    label .m1.txt04  -background blue -fg white -text "DRIVES"
    label .m1.txt05  -background blue -fg white -text "SITE"
    label .m1.txt08  -background blue -fg white -text "IF Attenuators"
    label .m1.val02  -textvariable val02 -background white -fg black
    label .m1.val03  -textvariable val03 -background white -fg black
    label .m1.val04  -textvariable val04 -background white -fg black
    label .m1.val05  -textvariable val05 -background white -fg black
    label .m1.val06  -textvariable val06 -background white -fg black
    label .m1.val07  -textvariable val07 -background white -fg black
    label .m1.val08  -textvariable val08 -background white -fg black
    pack .m1 -side left  -fill both
    pack .m1.txt05
    pack .m1.val06 -fill both
    pack .m1.txt02
    pack .m1.val07 -fill both
    pack .m1.txt03
    pack .m1.val03 -fill both
    pack .m1.txt04
    pack .m1.val04 -fill both
    pack .m1.val05 -fill both
    pack .m1.txt08
    pack .m1.val08 -fill both
}
# Setup display window to the left
displayWin

# Just a Timer
proc displayTimer {} {
    global val01
    label .m1.txt01  -background blue -fg white -text "UT:"
    label .m1.val01  -textvariable val01 -background white -fg black
    pack .m1.txt01
    pack .m1.val01 -fill both

}
# Setup FS clock
# ==========================================================================
#
#           COOK UP THE ITEMS UNDER THE BUTTONS SECTION
#
# ==========================================================================
# File menu
menu .m.file.menu  -background blue -fg white
.m.file.menu add command -label Load  -command LoadFile
.m.file.menu add command -label Save  -command SaveFile

menu .m.dsp.menu -background blue -fg white
.m.dsp.menu add command -label General  -command {DSPit monit2}
.m.dsp.menu add command -label Time  -command {DSPit monit1}
.m.dsp.menu add command -label Tsys  -command {DSPit monit3}
.m.dsp.menu add command -label Weather  -command {exec $LIBDEF/vue_fswx $fontsize &}

# Help menu
menu .m.help.menu -background blue -fg white
.m.help.menu add command -label "Logo"  -command {exec $LIBDEF/vue_logo &}
.m.help.menu add command -label "About fsvue"  -command {Help VUE}
.m.help.menu add command -label "Using fsvue"  -command {Help using}
.m.help.menu add command -label "Menus"  -command {Help menus}
.m.help.menu add command -label "Aliases & Commands"  -command {Help commands}
 
# Command menu
menu .m.cmd.menu -background blue -fg white
.m.cmd.menu add command -label Add  -command AddCmd
.m.cmd.menu add separator

# ==========================================================================
#
#           COOK UP PROCEDURES SECTION
#
# ==========================================================================
proc forAllMatches { w pattern script} {
    scan [$w index end] %d numLines
    for {set i 1} {$i < $numLines} {incr i} {
#$w mark set last $i.0
	$w mark set last $i.0
	while { [regexp -indices $pattern \
		[$w get last "last lineend"] indices]} {
	    $w mark set first \
			"last + [lindex $indices 0] chars"
		$w mark set last "last + 1 chars \
			+ [lindex $indices 1] chars"
		uplevel $script
	    }
	}
    }
# --------------------------------------------------------------------------
# setup display area( text and scrollbar) to monitor Field System.
# This will be pipeing information from the station.log file.
# --------------------------------------------------------------------------
# 
text .t -yscrollc {.sb set}  -bg linen
scrollbar .sb -bg linen -command {.t yview}
text .t2 -height 1  -bg white -fg Red

.t tag configure errtag -foreground black -background red

pack .sb -side right -fill y
pack .t .t2 -side top -fill both -expand 1

# --------------------------------------
# Update display area with line of text
# --------------------------------------
#
proc line {text} {
    global newfile_filename val02 val03 val04 val05 val06 val07 val08
    global stopscroll

    set val02 $newfile_filename
    set val03 [exec vueinfo rack]
    set val04 [exec vueinfo drive1]
    set val05 [exec vueinfo drive2]
    set val06 [exec vueinfo site]
    set val07 [exec vueinfo lskd]
    set val08 [exec vueinfo ifs]
    .t insert end $text\n; 
    .t see end; 
    .t yview end
    update idletask
}

# -----------------------------------------------
# Monitor fd (inpipe) and feed into display area
# -----------------------------------------------
#
proc logOutput {inpipe cmd} {
    global newfile_filename pids 

     if {[eof $inpipe]} {
	line "end of <$cmd>, or file may not exist in /usr2/log.\nSelect a valid log file by clicking on Log button."
	catch {close $inpipe}
    } else {
	set new [exec vueinfo log] 
	if {[string compare $new $newfile_filename] != 0} {
	    set newfile_filename [lindex $new 0]
	    AnotherLog
	}
	line [gets $inpipe]
    }
    # Mark error messages.
    forAllMatches .t "ERROR" {
	.t tag add big first last
    }
    .t tag configure big -background Red -foreground white -borderwidth 2 \
	    -font -Adobe-Helvetica-Medium-R-Normal--*-190-* \
	    -relief sunken
#    bell -displayof .t
    .t2 delete 1.0 end
    .t2 insert end "You are now viewing $newfile_filename"
}

# -------------------
# Setup fd monitoring 
# -------------------
#
proc execAndLog {cmd} {
    global newfile_filename inpipe pids

    set inpipe [open "|/bin/sh -c \"$cmd &1; :\""]
    set pids [pid $inpipe]
    fileevent $inpipe readable [list logOutput $inpipe $cmd]
 }

# -------------------
# Close fd monitoring 
# -------------------
#
proc execAndLogClose {} {
    global newfile_filename pids
    incr pids 1
    catch {exec kill $pids}
#    exec kill $pids
#    exec vue &
    exit
}

# -------------------
# -- mainline this was to test the loop ---
# -------------------
#
set newfile_filename [exec vueinfo log] 
execAndLog "tail -f -n 24 /usr2/log/$newfile_filename"

# -----------------------------------------
# setup a display monitor on a growing file
# -----------------------------------------
#
proc AnotherLog {} {
    global inpipe newfile_filename pids
    incr pids 1
    catch {exec kill $pids}
 #    exec kill $pids
    execAndLog "tail -f -n 24 /usr2/log/$newfile_filename"
}

# ------------------------------------------
#text: read a FS log file into a text widget
# ------------------------------------------
#
proc loadFile file {
    global newfile_filename
    .t delete 1.0 end
    set f [open $file r]
    while { ![eof $f] } {
	.t insert end [read $f 1000]
	.t see end; 
	.t yview end
    }

# Mark error messages.
forAllMatches .t "ERROR" {
    .t tag add big first last
}
.t tag configure big -background Red -foreground white -borderwidth 2 \
	-font -Adobe-Helvetica-Medium-R-Normal--*-190-* \
	-relief sunken
    close $f
    .t2 delete 1.0 end
    .t2 insert end "You are now viewing $newfile_filename"
}

.t tag bind big <Enter> {
    .t tag configure big -background lightblue
}

.t tag bind big <Leave> {
    .t tag configure big -background Red
}

# help from WWW for FS installation and other things.
#.t tag bind big <Button-2> {
#    open_html lupus.gsfc.nasa.gov/fs
#}


# ==========================================================================
#
#          CONTINUE COOKING PROCEDURES SECTION
#
# ==========================================================================
#
proc open_html {file} {
    global tcl_platform

    switch $tcl_platform(platform) {
	unix { 
	    set cmd "exec netscape -remote \"openFile($file)\""
	    if {[catch $cmd] !=0} {
		exec netscape &
		while {[catch $cmd] !=0} {
		    after 500
		}
	    }
	}
	window {
	    set cmd [list exec netscape $file &]
	    if {[catch $cmd] !=0} {
		set prog [tk_getOpenFile -title "Where is Netscape?"]
		if {$prog != " "} {
		    exec $prog $file &
		}
	    }
	}
    }
}

# ---------------------
# Field System Displays
# ---------------------
#
proc DSPit dspname {
    if {$dspname == "monit1"} {
	exec xterm -geom 38x1+0+0 -bg linen -fg black -sb \
		-sl 1000 -e /usr2/fs/bin/$dspname &
    }
    if {$dspname=="monit2"} {
	exec xterm -geom 90x10+100+0 -bg linen -fg black \
		-sb -sl 1000 -e /usr2/fs/bin/$dspname &
    }
    if {$dspname=="monit3"} {
	exec xterm -geom 38x18+500+40 -bg linen -fg black \
		-sb -sl 1000 -e /usr2/fs/bin/$dspname &
    }
    if {$dspname=="errchk"} {
	exec xterm -geom 90x10+90+0 -bg linen -fg black \
		-sb -sl 1000 -e /usr2/fs/bin/$dspname &
    }
}

# ---------------------------------------------------------------
# Routine to load the Command Menu dynamically from the cmd array
# each menu entry will call the procedure ExFromMenu with an
# argument of the command name
# ---------------------------------------------------------------
#
proc LoadCmdMenu {} {
        global cmd bfont

        foreach name [lsort [array names cmd]] {
                set c "ExFromMenu $name"
	    .m.cmd.menu add command -label $name  -command $c
        }
}

# -------------------------------------
# Load the command menu at startup time
# -------------------------------------
#
LoadCmdMenu

# -------------------------------------------------------
# That's all the top level windows
# Now we have a bunch of actions to do things
#
# The execute routine. Called when Return is typed in the
# main entry window and also from the Command Menu
# -------------------------------------------------------
#
proc Execute {} {
        global cmd
        
        set str [.m.ent get]
        set str "[MinMatch $str]"
        if { $str == "" } {
                return
        }
        set val $cmd($str)
        if { $val == "" } {
                return
        }
        
        switch $val {
        quit    {       exit    }
        default {
                       .m.ent delete 0 end
                        eval exec $val &
                }
        }
}

# ----------------------------
# Minimum length matching code
# ----------------------------
#
proc MinMatch { str } {
        global cmd
        
        set found ""
        foreach name [lsort [array names cmd]] {
                if { [string match $str* $name] } {
                        if { $found != "" } {
                                return ""
                        } else {
                                set found $name
                        }
                }
        }
        return $found
}

# -------------------------------------------------
# Called from the Command menu to execute a command
# -------------------------------------------------
#
proc ExFromMenu {name} {

        .m.ent delete 0 end
        .m.ent insert end $name
        Execute
}

# ----------------------------------------------------------
# Load the cmd array from a file
# currently this is called .xfscmds in the current directory
# You should change this
# ----------------------------------------------------------
#
proc LoadFile {} {
        global cmd xfsfile

        if { [file exists $xfsfile] } {
                set f [open $xfsfile r]
                while { [gets $f line] >= 0 } {
                        set line [split $line ":"]
                        set new [lindex $line 0]
                        set cmd($new) [join [lrange $line 1 end] ":"]
                }
                close $f
        }
        .m.cmd.menu delete 2 last
        LoadCmdMenu

}

# ----------------------------------
#       Executed as we start working
# ----------------------------------
#
LoadFile

# --------------------------
# The converse - save a file
# --------------------------
#
proc SaveFile {} {
        global cmd xfsfile

        set f [open $xfsfile w]
        foreach name [lsort [array names cmd]] {
                puts $f "$name:$cmd($name)"
        }
        close $f
}

# ---------------------------------------------
# Called from the Add entry in the command menu
# creates a pop-up window
# ---------------------------------------------
#
proc AddCmd {} {
        global cmd bfont

        # on error kill this
        catch {destroy .pop}
        # make a toplevel item
        toplevel .pop -bg linen

        # add a title for the Window Manager to use
        wm title .pop "Add Command"
        
        # the outer frame
        frame .pop.f -bd 2
        pack .pop.f -side top -fill both

        # The frame is a title, and entry box, another title and another
        # entry box
        label .pop.f.lab1 -text "Command Name" 
        entry .pop.f.e1 -relief sunken
        label .pop.f.lab2 -text "Command" 
        entry .pop.f.e2 -relief sunken
        pack .pop.f.lab1 .pop.f.e1 .pop.f.lab2 .pop.f.e2 -side top
        # The bottom of the frame has two buttons, one to get rid of this
        # diaglogue box, one to load the cmd vector
        frame .pop.f.m -bd 2 -bg linen
        pack .pop.f.m -fill x -expand yes -side top

        button .pop.f.m.d -text "Dismiss"  -command PopDown
        button .pop.f.m.l -text "Load"  -command LoadIt
        pack .pop.f.m.d .pop.f.m.l -side left -expand yes -fill x

        # we set the focus to the top box, when we type return in that
        # the cursor goes to the second nox
        bind .pop.f.e1 <Return> {focus .pop.f.e2}
        # a return here means load the command array
        bind .pop.f.e2 <Return> {LoadIt}

        # now get the geometry right.
        # this is somewhat magic, but the idea is to remove it from
        # the screen, work out where to place it and then make it
        # visible again
        wm withdraw .pop
        update idletasks
        set xy [split [wm geom .] "+x"]
        set x [lindex $xy 2]
        set y [lindex $xy 3]
        set x [expr $x + 10 ]
        set y [expr $y + 10 ]
        wm geom .pop "+$x+$y"
        wm deiconify .pop

        # save the old focus
        set oldFocus [focus]
        # stop the main window taking events, nothing can happen there until
        # we leave from this window
        grab .pop
        # set the focus
        focus .pop.f.e1
        # wait until this top level window dies
        tkwait window .pop
        # when that happens reset the focus     
        focus $oldFocus
        # load the Command menu and resume
        LoadCmdMenu
	# reload so as not the see two listings 
	LoadFile
}

# ---------------------------------------------------
# Called from the Dismiss button, just kill the popup
# ---------------------------------------------------
#
proc PopDown {} {
        destroy .pop
}

# ------------------------------------------------------------
# Load the cmd vector from the contents of the two entry boxes
# and kill the popup
# ------------------------------------------------------------
#
proc LoadIt {} {
        global cmd

        set c [.pop.f.e1 get]
        set v [.pop.f.e2 get]
        if { $c != "" && $v != "" } {
                set cmd($c) $v
        }
        PopDown
}

# --------------------
#       Help procedure
# --------------------
#
#
proc Help {sel} {
        global cmd
# long lines to allow msg to format things

       set h(VUE) {Field System VLBI User Environment (fsvue)

Pronounced: 'f' 's' 'view' or just 'view'.

 fsvue is a Tcl/Tk program that monitors the Field System. It places emphasis on errors generated by the FS by making them the color 'RED'. fsvue also has other features.

The following are features included in fsvue:

[i]
Gives you information on fsvue.

[File]
This will allows you to [Load] the stored commands from a file called .xfscmds in your home directory, it also has an option [Save] commands added by the [Add] command under the button [Execute].

[Displays]
This has a menu of popup displays used by the Field System. General, Time, Tsys, Errors, and Weather.
           
[Execute]
This will display a menu with all the known command names and also allows you to add commands using the [Add] entry. This has a window next to it where you type a new keyword and command.

[Examine LOGS]
This will popup a window that will help you examine passed logs. Information on how to use it is under the [i] button in the popup window.

[Other Files]
This will popup a window that will help you examine Field System related files. Information on how to use it is under the [i] button in the popup window.

[Field System HELP]
This will popup a window that will contain the help files from the Field System.

[Exit] - Get out of fsvue.}

       set h(using) {To use fsvue command line window you simply type a keyword into the window. The text that you type is looked up in a table and translated into a command. This command is started, clearing the entry window.

fsvue will perform string matching on the text that you type, you only need to type the text that uniquely identifies the command you want to use.
You can load and store the internal table from a file called .xfscmds on your home directory, see About Menus}

        set h(menus) {The File menu allows you to load the stored commands from a file called .xfscmds on your home directory.
The Execute menu displays all the known command names and also allows you to add commands using the Add entry. This pops up a window where you type a new keyword and command.
The Help Menu prints Help.}

        if { $sel == "commands" } {
                set h(commands) {Key    Alias
	}
                foreach name [lsort [array names cmd]] {
                        set h(commands) \
                                [format "%s\n%-7s\t%s" $h(commands) $name $cmd($name)]
                }
        }
        tk_dialog .help "Information" $h($sel) {} -1 OK
}

# -----------------------------------------------------------------------------
# This is a standard bit of code, modified to use my font
# dialog.tcl --
#
# This file defines the procedure tk_dialog, which creates a dialog
# box containing a bitmap, a message, and one or more buttons.
#
# $Header: /user6/ouster/wish/library/RCS/dialog.tcl,v 1.4 93/08/16 16:59:52 ouster Exp $ SPRITE (Berkeley)
#
# Copyright (c) 1992-1993 The Regents of the University of California.
# All.vpeople.rights reserved.
#
# Permission is hereby granted, without written agreement and without
# license or royalty fees, to use, copy, modify, and distribute this
# software and its documentation for any purpose, provided that the
# above cop.vpeople.right notice and the following two paragraphs appear in
# all copies of this software.
#
# IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
# OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
# CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
# ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
# PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#

#
# tk_dialog:
#
# This procedure displays a dialog box, waits for a button in the dialog
# to be invoked, then returns the index of the selected button.
#
# Arguments:
# w -           Window to use for dialog top-level.
# title -       Title to display in dialog's decorative frame.
# text -        Message to display in dialog.
# bitmap -      Bitmap to display in dialog (empty string means none).
# default -     Index of button that is to display the default ring
#               (-1 means none).
# args -        One or more strings to display in buttons across the
#               bottom of the dialog box.
# ---------------------------------------------------------------------------
#
proc tk_dialog {w title text bitmap default args} {
    global tk_priv bfont

    # 1. Create the top-level window and divide it into top
    # and bottom parts.
    set ICONDIR /usr2/fs/fsvue/images

    catch {destroy $w}
    toplevel $w -class Dialog
    wm title $w $title
    wm iconbitmap $w @$ICONDIR/vlb.icon
    wm iconname $w {fsvue}
    frame $w.top -relief raised -bd 1 -bg navy
    pack $w.top -side top -fill both
    frame $w.bot -relief raised -bd 1 -bg navy
    pack $w.bot -side bottom -fill both

    # 2. Fill the top part with bitmap and message.

    message $w.msg -width 8i -text $text 
    pack $w.msg -in $w.top -side right -expand 1 -fill both -padx 5m -pady 5m
    if {$bitmap != ""} {
        label $w.bitmap -bitmap $bitmap
        pack $w.bitmap -in $w.top -side left -padx 5m -pady 5m
    }

    # 3. Create a row of buttons at the bottom of the dialog.

    set i 0
    foreach but $args {
        button $w.button$i -text $but -command "set tk_priv(button) $i"
        if {$i == $default} {
            frame $w.default -relief sunken -bd 1 -bg linen
            raise $w.button$i $w.default
            pack $w.default -in $w.bot -side left -expand 1 -padx 3m -pady 2m
            pack $w.button$i -in $w.default -padx 2m -pady 2m \
                    -ipadx 2m -ipady 1m
            bind $w <Return> "$w.button$i flash; set tk_priv(button) $i"
        } else {
            pack $w.button$i -in $w.bot -side left -expand 1 \
                    -padx 3m -pady 3m -ipadx 2m -ipady 1m
        }
        incr i
    }

    # 4. Withdraw the window, then update all the geometry information
    # so we know how big it wants to be, then center the window in the
    # display and de-iconify it.

    wm withdraw $w
    update idletasks
    set x [expr [winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
            - [winfo vrootx [winfo parent $w]]]
    set y [expr [winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
            - [winfo vrooty [winfo parent $w]]]
    wm geom $w +$x+$y
    wm deiconify $w

    # 5. Set a grab and claim the focus too.

#    set oldFocus [focus]
#    grab $w
#    focus $w

    # 6. Wait for the user to respond, then restore the focus and
    # return the index of the selected button.

    tkwait variable tk_priv(button)
    destroy $w
#    focus $oldFocus
    return $tk_priv(button)
}

# ==========================================================================
#
#                         FILESELECTION SECTION
#
# ==========================================================================
#
proc fileselection_dialog {} {
 
 global global_startmode current_DefaultDir newfile_filename current_filename

 set con 1
 set types {
  {{Log Files} {*.log} }
  {{All Files} {*} }
 }

 set current_filename [tk_getOpenFile -title "Open new log file" -filetypes $types -initialdir $current_DefaultDir]
# set newfile_filename $current_filename
# set newfile_filename [tk_getOpenFile -title "Open new log file" -filetypes $types -initialdir $current_DefaultDir]

 if {$newfile_filename != ""} {
     .t delete 1.0 end
      set $newfile_filename [lrange $current_filename 10 end]
     .t2 delete 1.0 end
     .t2 insert end "You are now viewing $newfile_filename"
      AnotherLog 
 }
}
